﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using buildingLib.daily;
using System.Data.SqlClient;
using System.Net;
using System.IO;

namespace _dmis.daily
{
    public partial class RasterFilesView : Form
    {
        RasterFiles m_files;
        
        //存储文件的字节数据        
        private int mark = 0;
        

        public RasterFilesView(Guid archiveID)
        {
            //this.Text = archive.Name;
            InitializeComponent();
            m_files =RasterFiles.GetRasterFiles(archiveID);
        }

        /// <summary>
        /// 窗口加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RasterFilesView_Load(object sender, EventArgs e)
        {
             
            this.bindingNavigatorPositionItem.Enabled = true;
            if (m_files.Count <= 1)
            {
                this.bindingNavigatorMovePreviousItem.Enabled = false;
                this.bindingNavigatorMoveNextItem.Enabled = false;
                this.bindingNavigatorMoveFirstItem.Enabled = false;
                this.bindingNavigatorMoveLastItem.Enabled = false;
            }
            else
            {
                this.bindingNavigatorMovePreviousItem.Enabled = false;
                this.bindingNavigatorMoveNextItem.Enabled = true;
                this.bindingNavigatorMoveFirstItem.Enabled = false;
                this.bindingNavigatorMoveLastItem.Enabled = true;
            }
            DisplayPic(mark);
        }

        /// <summary>
        /// 打开第一个文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bindingNavigatorMoveFirstItem_Click(object sender, EventArgs e)
        {
            mark = 0;
            this.bindingNavigatorMoveFirstItem.Enabled = false;
            this.bindingNavigatorMovePreviousItem.Enabled = false;
            this.bindingNavigatorMoveNextItem.Enabled = true;
            this.bindingNavigatorMoveLastItem.Enabled = true;
            DisplayPic(mark);
        }

        /// <summary>
        /// 向前移动一个文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bindingNavigatorMovePreviousItem_Click(object sender, EventArgs e)
        {
            mark = Convert.ToInt32(this.bindingNavigatorPositionItem.Text) - 1;
            if (mark < 0 || mark > m_files.Count)
            {
                MessageBox.Show("您的输入有误", "提示", MessageBoxButtons.OK);
                return;
            }
            if (mark == 0)
            {
                DisplayPic(mark);
                this.bindingNavigatorMovePreviousItem.Enabled = false;
                this.bindingNavigatorMoveLastItem.Enabled = true;
                this.bindingNavigatorMoveNextItem.Enabled = true;
                this.bindingNavigatorMoveFirstItem.Enabled = true;
                return;
            }
            mark--;
            if (mark == 0)
            {
                this.bindingNavigatorMovePreviousItem.Enabled = false;
            }
            else
            {
                this.bindingNavigatorMovePreviousItem.Enabled = true;
                this.bindingNavigatorMoveLastItem.Enabled = true; 
                this.bindingNavigatorMoveNextItem.Enabled = true;
                this.bindingNavigatorMoveFirstItem.Enabled = true;
            }
            DisplayPic(mark);
        }

        /// <summary>
        /// 向下一个文件移动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bindingNavigatorMoveNextItem_Click(object sender, EventArgs e)
        {
            try
            {
                mark = Convert.ToInt32(this.bindingNavigatorPositionItem.Text) - 1;
                if (mark < 0 || mark > m_files.Count)
                {
                    MessageBox.Show("您的输入有误", "提示", MessageBoxButtons.OK);
                    return;
                }
                if (mark == m_files.Count - 1)
                {
                    DisplayPic(mark);
                    this.bindingNavigatorMoveNextItem.Enabled = false;
                    this.bindingNavigatorMoveLastItem.Enabled = true;
                    this.bindingNavigatorMovePreviousItem.Enabled = true;
                    this.bindingNavigatorMoveFirstItem.Enabled = true;
                    return;
                }
                mark++;
                if (mark == m_files.Count - 1)
                {
                    this.bindingNavigatorMoveNextItem.Enabled = false;
                }
                else
                {
                    this.bindingNavigatorMoveLastItem.Enabled = true;
                    this.bindingNavigatorMoveNextItem.Enabled = true;
                    this.bindingNavigatorMovePreviousItem.Enabled = true;
                    this.bindingNavigatorMoveFirstItem.Enabled = true;
                }
                DisplayPic(mark);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK);
                return;
            }
            
        }

        /// <summary>
        /// 打开最后一个文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bindingNavigatorMoveLastItem_Click(object sender, EventArgs e)
        {
            mark = m_files.Count - 1;
            this.bindingNavigatorMoveLastItem.Enabled = false;
            this.bindingNavigatorMoveNextItem.Enabled = false;
            this.bindingNavigatorMovePreviousItem.Enabled = true;
            this.bindingNavigatorMoveFirstItem.Enabled = true;
            DisplayPic(mark);
        }

       
        

        /// <summary>
        /// 显示文件到PictureBox上
        /// </summary>
        /// <param name="mark"></param>
        private void DisplayPic(int mark)
        {
            

            string strFtp =  System.Configuration.ConfigurationManager.AppSettings["connectionFtp"];
            string[] ftpInfo = strFtp.Split(';');
            string ftpServer = "ftp://" + ftpInfo[0].Split('=')[1] + "/";
            string ftpUser = ftpInfo[1].Split('=')[1];
            string ftpPassWord = ftpInfo[2].Split('=')[1];
            string filepath = string.Format("{0}/{1}", System.IO.Path.GetDirectoryName(m_files[mark].FTPPath), m_files[mark].GUIDName);
            Uri uri = new Uri(ftpServer + filepath);
            WebClient request = new WebClient();

            request.Credentials = new NetworkCredential(ftpUser, ftpPassWord);

            byte[] newFileData = request.DownloadData(uri.ToString());

            MemoryStream ms = new MemoryStream(newFileData);
            Image returnImage = Bitmap.FromStream(ms);
            //returnImage.RotateFlip(RotateFlipType.Rotate90FlipNone);           
            this.scalablePictureBox.Picture = returnImage;
            
            ms.Close();
            request.Dispose();
            //新增的放大、缩小的代码
            this.ActiveControl = this.scalablePictureBox.PictureBox;

           
            this.lblPicInfo.Text =m_files[mark].Name;

            this.bindingNavigatorPositionItem.Text = (mark + 1).ToString();
            this.bindingNavigatorCountItem.Text = string.Format("/{0}", m_files.Count.ToString());

            
        }

        /// <summary>
        /// 鼠标离开事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bindingNavigatorPositionItem_MouseLeave(object sender, EventArgs e)
        {
            CheckPositon();
        }

        /// <summary>
        /// 检查输入数据
        /// </summary>
        private void CheckPositon()
        {
            mark = Convert.ToInt32(this.bindingNavigatorPositionItem.Text.Trim()) - 1;
            if (mark < 0 || mark > m_files.Count)
            {
                MessageBox.Show("您的输入有误", "提示", MessageBoxButtons.OK);
                return;
            }
        }

        private void bindingNavigatorPositionItem_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                mark = Convert.ToInt32(this.bindingNavigatorPositionItem.Text.Trim()) - 1;
                DisplayPic(mark);
            }
        }

        //图像旋转
        private void btnXZ_Click(object sender, EventArgs e)
        {
            Bitmap bmp_cloned = new Bitmap(this.scalablePictureBox.PictureBox.Image as Bitmap);
            bmp_cloned.RotateFlip(RotateFlipType.Rotate90FlipNone);

            this.scalablePictureBox.Picture = bmp_cloned;
            
            RefreshGraph();
        }

        //刷新图像
        private void RefreshGraph()
        {
            this.scalablePictureBox.Refresh();            
        }
    }
}
